<template>
  <div>
    <table>
      <tr>
        <td style="width: 50px;">诊断:</td>
        <td>
          <el-input type="textarea" v-model="reports.header.diagnose"></el-input>
        </td>
        <td style="width:80px;">诊断类型:</td>
        <td style="width:80px;">
          <el-select v-model="reports.diagnostic_type" style="width:80px;">
            <el-option label="初诊" value="0"></el-option>
            <el-option label="复诊" value="1"></el-option>
          </el-select>
        </td>
      </tr>
      <tr>
        <td>医嘱:</td>
        <td colspan=3>
          <el-input type="textarea" v-model="reports.header.doctor_advice"></el-input>
        </td>
      </tr>
    </table>
    <table class="addDrug">
      <tr>
        <th>序号</th><th>药品名称</th><th>药品含量/规格</th><th>单次用量</th>
        <th>用法</th><th>次/天</th><th>天数</th><th colspan=2>总量</th><th>单价</th><th>操作</th>
      </tr>
      <tr v-for="row in reports.detail">
        <td>{{ row.index }}</td>
        <td>
          <el-autocomplete
            v-model="row.name"
            size="small"
            :fetch-suggestions="querySearchDrug"
            placeholder="药品搜索"
            :trigger-on-focus="false"
            @select="handleSelectDrug">
          </el-autocomplete>
        </td>
        <td>{{ row.content_unit }}/{{ row.standard }}</td>
        <td><input style="width: 60px;" v-model="row.dosis"></input></td>
        <td><input style="width: 70px;" v-model="row.usage"></input></td>
        <td><input style="width: 40px;" v-model="row.times"></input></td>
        <td><input style="width: 40px;" v-model="row.day"></input></td>
        <td><input style="width: 40px;" v-model="row.product_uom_qty"></input></td>
        <td>
          <select v-model="row.product_uom">
            <option
              v-for="item in row.count_unit"
              :label="item.label"
              :value="item.value">
            </option>
          </select>
        </td>
        <td>
          <input style="width: 40px;" v-model="row.price_unit"></input>
          <div v-for="item in row.count_unit" style="display: none;">
            <div v-if="row.product_uom == item.value">{{ row.price_unit=item.price }}</div>
          </div>
        </td>
        <td>
          <el-button icon="delete" size="small" @click.native="deleteLine(row.index)"></el-button>
        </td>
      </tr>
    </table>
    <span>
      <el-button type="primary" @click.native="IncreaseLine">增加药品</el-button>
      <el-button type="primary" @click.native="addReport">保存处方</el-button>
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      reports: {
        header: {
          diagnose: '',
          doctor_advice: '',
          diagnostic_type: '0',
          prescription_type: '0',
        },
        detail: [
          { "index": 1,
            "product_id": "",
            "name": "",
            "content_unit": "",
            "standard": "",
            "dosis": "",
            "usage": "",
            "times": "",
            "day": "",
            "product_uom_qty": 1,
            "product_uom": "",
            "count_unit": [],
            "price_unit": "",
            "use_level": ""
          }
        ]
      },
      addLoading: false
    }
  },
  props: {
    basicInfo: {
      type: Object,
      default: {
        partner_id: 0,
        partner_name: '',
        sex: '',
        age: '',
        birthday: '',
        phone: '',
        street: '',
        allergic_note: '',
      }
    },
    doctorInfo: null,
    currentTime: null,
    allergics: {
      type: Array,
      default: []
    }
  },
  methods: {
    addReport() {
      let vm = this
      vm.addLoading = true
      vm.reports.header = Object.assign(vm.reports.header,vm.basicInfo,vm.doctorInfo)
      vm.reports.header.d_time = vm.currentTime
      this.$http.post('/api/prescriptions/augment', vm.reports).then((response) => {
        this.addLoading = false
        this.$message({
          showClose: true,
          message: '添加成功',
          type: 'success'
        })
      }, (response) => {
        this.addLoading = false
        this.$message({
          showClose: true,
          message: '服务器连接失败',
          type: 'error'
        })
      })
    },
    querySearchDrug(queryString, funcd) {/*查找药品*/
      if (queryString == '') {
        funcd()
      }
      else {
        this.$http.get('/api/drugs/'+queryString+'/1').then(function (response) {
          let data = []
          let datas = response.data.data
          for (let i = 0; i < datas.length; i++) {
            data[i] = { 'value': datas[i].name, 'id': datas[i].id , 'allergic_history': datas[i].allergic_history }
          }
          if (data.length > 0) {
            funcd(data)  
          } else {
            data[0] = { 'value': '' }
            funcd(data)
          }          
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
          funcd()
        })      
      }
    },
    handleSelectDrug(val) {
      if (this.allergics[0].drug_allergic == '无') {
        this.addDrugInfo(val)
      } else {
        let vm = this
        let VerifyDrug = this.allergics.map(function(obj) {
          if (obj.key == 1 && obj.drug_id == val.id) {
            vm.$alert('该患者'+val.value+'过敏，请不要开此药', '提示', { type: 'warning' })
            return false
          } else if (obj.key == 2 && obj.drug_id == val.allergic_history[0]) {
            vm.$alert('该患者'+val.allergic_history[1]+'过敏，请不要开此药', '提示', { type: 'warning' })
            return false
          } else {
            return true
          }
        }).indexOf(false)
        if (VerifyDrug == -1) {
          this.addDrugInfo(val)
        } else {
          for (let i = 0; i < this.reports.detail.length; i++) {
            if (this.reports.detail[i].name == val.value) {
              this.reports.detail[i].name = ''
            }
          }
        }
      }
    },
    // 用ID查询药品
    addDrugInfo(val) {
      this.$http.get('/api/drugs/id/'+val.id).then(function (response) {
        var data = response.data
        for (let i = 0; i < this.reports.detail.length; i++) {
          if (this.reports.detail[i].name == val.value) {
            this.reports.detail[i].product_id = data.id
            this.reports.detail[i].content_unit = data.drug_content
            this.reports.detail[i].standard = data.drug_spec
            this.reports.detail[i].dosis = data.use_level
            this.reports.detail[i].usage = data.usage[1]
            this.reports.detail[i].times = data.times[1]
            this.reports.detail[i].day = data.days
            this.reports.detail[i].count_unit = [{
              value: data.small_unit[0],
              label: data.small_unit[1],
              price: data.small_price
            }, {
              value: data.big_unit[0],
              label: data.big_unit[1],
              price: data.big_price
            }]     
            this.reports.detail[i].product_uom = data.small_unit[0]
          }
        }
      }, (response) => {
        this.$message({
          showClose: true,
          message: '服务器连接失败',
          type: 'error'
        })
      })
    },
    IncreaseLine() { /*增加行*/
      this.reports.detail.push({ 
        "index": 1,
        "product_id": "",
        "name": "",
        "content_unit": "",
        "standard": "",
        "dosis": "",
        "usage": "",
        "times": "",
        "day": "",
        "product_uom_qty": 1,
        "product_uom": "",
        "count_unit": [],
        "price_unit": "",
        "use_level": ""
      })
      this.SinceGrowth()
    },
    deleteLine(index) { /*删除行*/
      this.reports.detail.splice(index-1, 1)
      this.SinceGrowth()
    },
    SinceGrowth() { /*索引*/
      for(let i=0;i<this.reports.detail.length;i++) {
        this.reports.detail[i].index=i+1 
      }  
    }
  },
  watch: {
  }
}
</script>